/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package Tools;

//import LogParser.CommandsHistory;
import LogParser.CommandsHistory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author Иван
 */
public class CMDThread implements Runnable {

    public Thread t;
    private List<String> parsedInputData;
    private TModel model;
    private ACommand command;
    private BufferedReader reader;
    private Logger logger;
    private CommandsHistory hist;

    public CMDThread(CommandsHistory hist) {
        this.hist = hist;
        logger = Logger.getLogger(CMDThread.class);
        t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {


        reader = new BufferedReader(new InputStreamReader(System.in));
        String tempIO;
        while (true) {
            try {
                model = TModel.getInstance();
                model.setInitCommandFactory(new FactorySingletonCommandConsole());
                System.out.print(model.getWorkDirectory() + ">");

                tempIO = reader.readLine();
                parsedInputData = model.parsingInputData(tempIO);

                // logger.info(model.getWorkDirectory() + ">" + tempIO);
                logger.info("<command>" + tempIO + "<command>");

                command = model.getInitCommandFactory().initializeCommand(parsedInputData);
                command.setParameters(parsedInputData);
                command.action(null);

                tempIO = command.getPrintResult();
                System.out.print(tempIO);
                logger.info("command is complete");


                System.out.println(hist.next());
                System.out.println(hist.next());
                System.out.println(hist.back());
                System.out.println(hist.back());
                System.out.println(hist.next());
                System.out.println(hist.next());

            } catch (Exception e) {
                System.out.print(e.toString() + "\n");
                logger.error(e.toString(), e);
            }
        }
    }
}
